package xyz.coolcsm.security.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xyz.coolcsm.security.config.JsonResult;
import xyz.coolcsm.security.config.ResultTool;
import xyz.coolcsm.security.config.RsaKeyProperties;
import xyz.coolcsm.security.entity.Comment;
import xyz.coolcsm.security.entity.Payload;
import xyz.coolcsm.security.entity.SysUser;
import xyz.coolcsm.security.service.CommentService;
import xyz.coolcsm.security.utils.JwtUtils;
import xyz.coolcsm.security.utils.UUIDUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * @author 什锦
 * @since 2021-06-1
 */

@RestController
public class CommentController {
    @Autowired
    CommentService commentService;

    @Autowired
    private RsaKeyProperties prop;

    //更新较快，不用缓存
    @GetMapping("/api/question/comment/{qid}/{limit}/{page}/{tcid}")
    public JsonResult getComment(@PathVariable("qid") Integer qid,@PathVariable("page") int page,@PathVariable("limit") int limit,
                                 @PathVariable("tcid") Integer tcid ){
        //评论是博客还是问题
        if (page < 1){
            page = 1;
        }
        Page<Comment> commentByQid = commentService.pageByQid(qid,limit, page,tcid);
        if (commentByQid!=null){
            return ResultTool.success(commentByQid);
        }
        return ResultTool.fail();
    }

    //个人的评论分页
    @GetMapping("/api/user/comment/{limit}/{page}")
    public JsonResult getCommentByUser(HttpServletRequest request,@PathVariable("page") int page,@PathVariable("limit") int limit){
        //获取个人信息
        String token = request.getHeader("Authorization");
        Payload<SysUser> payload = JwtUtils.getInfoFromToken(token.replace("Bearer ", ""),prop.getPublicKey(), SysUser.class);
        if (page < 1){
            page = 1;
        }
        //查询结果并返回
        Page<Comment> pageParam = new Page<>(page, limit);
        commentService.page(pageParam,new QueryWrapper<Comment>().eq("user_id",payload.getUserInfo().getId()));
        return ResultTool.success(pageParam);
    }

    //删除评论
    @DeleteMapping("/api/user/comment/delete/{qid}")
    public JsonResult delete(HttpServletRequest request,@PathVariable("qid") String qid){
        String token = request.getHeader("Authorization");
        Payload<SysUser> payload = null;
        payload = JwtUtils.getInfoFromToken(token.replace("Bearer ", ""),
                prop.getPublicKey(), SysUser.class);
        Boolean flag =commentService.remove(new QueryWrapper<Comment>().eq("id", qid).eq("user_id",payload.getUserInfo().getId()));
        //删除失败，返回失败结果
        if ( flag == false){
            return ResultTool.fail();
        }
        return ResultTool.success();
    }

    //新增评论并刷新评论
    @PostMapping("/api/question/comment")
    public JsonResult commentQuestion(HttpServletRequest request, @RequestBody Comment commentForm) {
        //获取个人信息
        String token = request.getHeader("Authorization");
        if (commentForm.getContent().length()==0||commentForm.getContent()==null){
            return ResultTool.fail();
        }
        Payload<SysUser> payload = JwtUtils.getInfoFromToken(token.replace("Bearer ", ""),
                        prop.getPublicKey(), SysUser.class);
        SysUser user = payload.getUserInfo();
        //添加评论人信息
        commentForm.setUserId(user.getId().toString());
        commentForm.setUserName(user.getNickName());
        commentForm.setUserAvatar(user.getAvatar());
        commentForm.setCommentId(UUIDUtils.getUuid());
        commentService.save(commentForm);
        Page<Comment> commentByQid = commentService.pageByQid(Integer.valueOf(commentForm.getQid()), 4, 1,commentForm.getTopicCategory());
        return ResultTool.success(commentByQid);
    }

}
